package cn.itcast.core.web.controller;

import cn.itcast.core.bean.TestCase;
import cn.itcast.core.service.TestCaseService;
import cn.itcast.core.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class TestCaseContorller {
    @Autowired
    TestCaseService testCaseService;

    @RequestMapping(value = "/testcase")
    //redirect 重定向
    public String showCumtomer() {
        return "redirect:/testcase/selectTestCaseList.action";
    }

    @RequestMapping("/testcase/select")
    @ResponseBody
    public TestCase selectCaseById(Long id) {
        TestCase testCase = testCaseService.findCaseListById(id);
        return testCase;
    }

    @RequestMapping("/testcase/update")
    @ResponseBody
    public String updateCase(TestCase testCase) {
        int res = testCaseService.updateCase(testCase);
        String message;
        if (res == 1) {
            message = "更新成功";
        } else {
            message = "更新失败";
        }
        return message;
    }

    @RequestMapping("/testcase/delete")
    @ResponseBody
    public String deleteCase(Long id) {
        int res = testCaseService.deleteCase(id);
        String message;
        if (res == 1) {
            message = "更新成功";
        } else {
            message = "更新失败";
        }
        return message;
    }

    @RequestMapping("/testcase/adit")
    @ResponseBody
    public String insertCase(TestCase testCase) {
        int res = testCaseService.insertinsertSelectiveCase(testCase);
        String message;
        if (res == 1) {
            message = "更新成功";
        } else {
            message = "更新失败";
        }
        return message;
    }

    @RequestMapping("/testcase/selectByTestCase")
    public ModelAndView selectByTestCase(String interfaceName, String interfaceType, String textResult, ModelAndView model) {
        List<TestCase> testCaseList = testCaseService.selectByTestCase(interfaceName, interfaceType, textResult);
        model.addObject(testCaseList);
        model.setViewName("testCase");
        return model;
    }

    @RequestMapping("/testcase/selectTestCaseList")
//分页查询 required = true 是必传的，一定在注解里
    public ModelAndView selectTestCaseList(@RequestParam(required = true, defaultValue = "1") int currPage, @RequestParam(defaultValue = "10") int pageSize, String interfaceName, String interfaceType, String textResult) {
        ModelAndView modelAndView = new ModelAndView();
        Page<TestCase> page = testCaseService.paging(interfaceName, interfaceType, textResult, currPage, pageSize);
        //将获取的page对象传入modelAndView中并命名为yeMianShuJu
        modelAndView.addObject("yeMianShuJu",page);
        //告诉SpringMVC 在web-inf 下jsp文件的名称
        modelAndView.setViewName("testCase");
        return modelAndView;
    }
}